
#include "stdafx.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;
    
    // initialize MFC and print and error on failure
    if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
    {
        // TODO: change error code to suit your needs
        cerr << _T("Fatal Error: MFC initialization failed") << endl;
        nRetCode = 1;
    }
    else
    {

        if (argc < 3) 
        {
			printf("Usage: bintohex src_bin_file dest_array_file\r\n");
            return 1;
		}

        CString strName;		
        
        CFile F1;	
        CFile F2;
        unsigned char* pVlc;
        if (F1.Open((const char*)argv[1],CFile::modeRead))
        {			
            if (F2.Open((const char*)argv[2], CFile::modeWrite|CFile::modeCreate))
            {
                long Len = F1.GetLength();
                pVlc = (unsigned char*)malloc(Len);
                F1.Read(pVlc,Len);
                
                //F2.Write("const uint8 bin_to_hex[] =\r\n{\r\n", sizeof ("const uint8 bin_to_hex[] =\r\n{\r\n") - 1);
                for(int i=0 ; i< Len; i++)
                {
                    if((i + 1) % 16 == 0)
                    {
                        strName.Format("0x%02x,\r\n", pVlc[i]);
                    }
                    else if ((i + 1) % 16 == 1)
                    {
                        strName.Format("    0x%02x,", pVlc[i]);
                    }
                    else
                    {
                        strName.Format("0x%02x,", pVlc[i]);
                    }
                    int testlen = strName.GetLength();
                    F2.Write(strName.GetBuffer(100), testlen );
                }
                //F2.Write("\r\n};\r\n", sizeof ("\r\n};\r\n") - 1);
                
                F2.Close();
                free(pVlc);
            }
            else
            {
                F1.Close();
            }
        }
    }
    
    return nRetCode;
}


